#******************************************************************
#
# Copyright 2016 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import os
import os.path
from tools.scons.RunTest import run_test

##
# CoAP-HTTP Proxy Unit Test build script
##
# SConscript file for Local PKI google tests
gtest_env = SConscript('#extlibs/gtest/SConscript')
CoAP_test_env = gtest_env.Clone()

if CoAP_test_env.get('RELEASE'):
    CoAP_test_env.AppendUnique(CCFLAGS=['-Os'])
else:
    CoAP_test_env.AppendUnique(CCFLAGS=['-g'])

#SConscript('#service/third_party_libs.scons', 'lib_env')

######################################################################
#unit test setting
######################################################################
#CoAP_test_env = lib_env.Clone()
target_os = CoAP_test_env.get('TARGET_OS')

######################################################################
# Build flags 'coap_http_proxy','oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap', 'cjson'])
######################################################################

CoAP_test_env.AppendUnique(LIBS=[
    'coap_http_proxy',
    'octbstack',
    'connectivity_abstraction',
    'coap',
    'cjson',
])

if target_os not in ['windows']:
    CoAP_test_env.AppendUnique(CXXFLAGS=['-O2', '-g', '-Wall', '-fmessage-length=0'])

CoAP_test_env.AppendUnique(CPPPATH=[
    '#/extlibs/hippomocks/hippomocks',
    '../include',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/csdk/include',
    '#/resource/csdk/logger/include',
    '#/resource/csdk/stack/include',
    '#/resource/csdk/connectivity/common/inc/',
    '#/resource/csdk/connectivity/lib/libcoap-4.1.1',
    '#/extlibs/cjson',
])

if CoAP_test_env.get('SECURED') == '1':
    CoAP_test_env.AppendUnique(CPPPATH=['#/resource/csdk/security/include/'])

with_upstream_libcoap = CoAP_test_env.get('WITH_UPSTREAM_LIBCOAP')
if with_upstream_libcoap == '1':
    CoAP_test_env.AppendUnique(CPPPATH=['#/extlibs/libcoap/libcoap/include'])
else:
    CoAP_test_env.AppendUnique(
        CPPPATH=['#/resource/csdk/connectivity/lib/libcoap-4.1.1/include'])

#CoAP_test_env.AppendUnique(CPPPATH = ['#/service/resource-encapsulation/include'])
#CoAP_test_env.AppendUnique(CPPPATH = ['../../resource-encapsulation/src/common/primitiveResource/include'])
CoAP_test_env.AppendUnique(
    CPPPATH=['#/service/resource-encapsulation/src/common/utils/include'])

if target_os in ['linux']:
    CoAP_test_env.AppendUnique(LIBS=['pthread', 'curl'])

if CoAP_test_env.get('SECURED') == '1':
    CoAP_test_env.AppendUnique(LIBS=[
        'mbedtls',
        'mbedx509',
        'mbedcrypto',
    ])
######################################################################
# Build Test
######################################################################

CoAP_unit_test_src = CoAP_test_env.Glob('./CoAPHttpUnitTest.cpp')
CoAP_unit_test = CoAP_test_env.Program('CoAP_unit_test', CoAP_unit_test_src)
Alias("CoAP_unit_test", CoAP_unit_test)
CoAP_test_env.AppendTarget('CoAP_unit_test')
CoAP_test_env.UserInstallTargetExtra(CoAP_unit_test, 'tests/service/coap-http-proxy')

if CoAP_test_env.get('TEST') == '1':
    if target_os in ['linux']:
        run_test(CoAP_test_env, '',
                 'service/coap-http-proxy/unittests/CoAP_unit_test',
                 CoAP_unit_test)
